package cn.itrigger.dynamic.sql.render;

import cn.itrigger.dynamic.sql.structure.SqlTable;

import java.util.*;

/**
 * 保存表与别名的关系
 * @author admin
 * @date 2018/9/21
 **/
public class TableAliasCalculator {

    private Map<SqlTable, String> aliases;

    protected TableAliasCalculator(Map<SqlTable, String> aliases) {
        this.aliases = Objects.requireNonNull(aliases);
    }

    public Optional<String> alias(SqlTable sqlTable) {
        return Optional.ofNullable(aliases.get(sqlTable));
    }

    public TableAliasCalculator putTablesAlias(SqlTable sqlTable, String alias) {
        aliases.put(sqlTable, alias);
        return this;
    }

    public static TableAliasCalculator of(Map<SqlTable, String> aliases) {
        return new TableAliasCalculator(aliases);
    }

    public static TableAliasCalculator empty() {
        return of(Collections.emptyMap());
    }

    public static TableAliasCalculator of(SqlTable sqlTable, String alias) {
        Map<SqlTable,String> aliases = new HashMap<>();
        aliases.put(sqlTable, alias);
        return of(aliases);
    }
}
